$(function(){
  var task={
     init:function(){
    	 var _this=this;
    	 _this.initDatagrid();
    	 _this.initEvent();
     },
     initEvent:function(){
	    var _this=this;
 		//添加实现
 		$("#add_task").click(function(){
 	        top.$.modalDialog({
                _form:"#taskForm", 
                _datagrid:_this.taskDataGrid,
 	            title : '用户信息添加',
 	            href:"/qrtz_task/add",
 	            onLoad:function(){
 	            	var f=top.$.modalDialog.handler.find("#taskForm"); 
 	            	f.form("reset");
 	            }
 	        }).dialog('open');
 		});
 		$("#edit_task").click(function(){
 			var selRows=_this.taskDataGrid.datagrid('getChecked');
 			JSmile.KEasy.edit(selRows,function(){
 	 	        top.$.modalDialog({
 	                _form:"#taskForm", 
 	                _datagrid:_this.taskDataGrid, 	 	        	
 	 	            title : '用户信息修改',
 	 	            href:"/qrtz_task/edit",
 	 	            onLoad:function(){
 	 	            	var f=top.$.modalDialog.handler.find("#taskForm"); 
 	 	            	f.form("reset");
 	 	            	f.form("load",selRows[0]);
 	 	            }
 	 	        }).dialog('open');
 			});
 		});
 		//删除实现
 		$("#del_task").click(function(){
 			var selRows=_this.taskDataGrid.datagrid('getSelections');
 			JSmile.KEasy.del('/qrtz_task/deleteByIds',selRows,function(res){
 				_this.taskDataGrid.datagrid('clearChecked').datagrid('reload');
 			});
 		});
 		//搜索实现
 		$("#search_task").click(function(){
 			_this.taskDataGrid.datagrid('load',$('#taskSearchForm').serializeJson());
 		}); 
     },
     initDatagrid:function(){//初始化各种datagrid
    	var _this=this;
    	_this.taskDataGrid=$('#task_datagrid').datagrid(JSmile.KEasy.initDatagrid({
	    	url:'/qrtz_task/findAllByPage',
	    	toolbar:'#task_toolbar',
	    	columns:[[
	   		 {field:'id',hidden:true},
			 {field:'ck',checkbox:true},
			 {field:'jobName',title:'任务名称',align:'center',width:100},
			 {field:'jobGroup',title:'任务分组',align:'center',width:100},
			 {field:'taskStatus',title:'任务状态',align:'center',width:100,sortable:true,formatter: function(value,row,index){
				 if("1"==value){
					 return "正常";
				 }else if("0"==value){
					 return "暂停";
				 }
			 }},
			 {field:'cron',title:'表达式',align:'center',width:100},
			 {field:'createDate',title:'创建时间',align:'center',width:100,sortable:true},
			 {field:'taskType',title:'任务类型',align:'center',width:100,sortable:true,formatter: function(value,row,index){
				 if("01"==value){
					 return "周期任务";
				 }else if("02"==value){
					 return "定时任务";
				 }
			 }},
			 {field:'isConcurrent',title:'是否有状态',align:'center',width:80,sortable:true,formatter: function(value,row,index){
				 return JSmile.KStatus.initStatus(value);
			 }},
			 {field:'remark',title:'备注',align:'center',width:100},
			 {field:'action',title:'操作',align:'center',width:100,formatter: function(value,row,index){
				 if(row.taskStatus=='0'){
					 var tem=JSmile.Kit.formatString('<a class="easyui-tooltip" style="cursor:pointer;padding:3px;" title=\"恢复\" onclick="resumeJob(\'{0}\',\'{1}\',\'{2}\');" ><i style="color:#5eb95e !important" class="iconfont sn-huifu"></i></a>', row.id,row.jobName,row.jobGroup);
				     return tem;
				 }else if(row.taskStatus=='1'){
					 var tem=JSmile.Kit.formatString('<a class="easyui-tooltip" style="cursor:pointer;padding:3px;" title=\"立即执行\" onclick="runJob(\'{0}\',\'{1}\',\'{2}\');" ><i style="color:#5eb95e !important" class="iconfont sn-run"></i></a>', row.id,row.jobName,row.jobGroup);
					 tem+=JSmile.Kit.formatString('<a class="easyui-tooltip" style="cursor:pointer;padding:3px;" title=\"暂停\" onclick="pauseJob(\'{0}\',\'{1}\',\'{2}\');" ><i style="color:##F37B1D !important" class="iconfont sn-stop"></i></a>', row.id,row.jobName,row.jobGroup);
					 return tem;
				 }
			 }}
		]]
	    }));
     }
  };
  task.init();
});
/*
 * 暂停任务
 */
function pauseJob(id,jobName,jobGroup){
	top.$.messager.alert("任务", "是否暂停该任务!", "info",function(){
		JSmile.KHttp.send({
			url:"/qrtz_task/pauseJob",
			data:{"id":id,"jobName":jobName,"jobGroup":jobGroup,"taskStatus":"0"}
		}).done(function(res){
			top.$.messager.alert("任务", res.errMsg?res.errMsg:"暂停任务成功!", "info");
			$('#task_datagrid').datagrid('reload');
		});
	});  
}
/*
 * 恢复任务
 */
function resumeJob(id,jobName,jobGroup){
	top.$.messager.alert("任务", "是否恢复该任务!", "info",function(){
		JSmile.KHttp.send({
			url:"/qrtz_task/resumeJob",
			data:{"id":id,"jobName":jobName,"jobGroup":jobGroup,"taskStatus":"1"}
		}).done(function(res){
			top.$.messager.alert("任务", res.errMsg?res.errMsg:"恢复任务成功!", "info");
			$('#task_datagrid').datagrid('reload');
		});
	});  
}
/*
 * 立即运行任务
 */
function runJob(id,jobName,jobGroup){
	top.$.messager.alert("任务", "是否立即运行该任务!", "info",function(){
		JSmile.KHttp.send({
			url:"/qrtz_task/runJob",
			data:{"id":id,"jobName":jobName,"jobGroup":jobGroup}
		},function(res){
			top.$.messager.alert("任务", res.errMsg?res.errMsg:"立即运行任务成功!", "info");
		});
	});  
}
